<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/1 0001
 * Time: 下午 2:48
 */
class ArticleService
{
    private $articleBodyModel;
    private $articleListModel;
    private $articleListCacheModel;
    private $articleTagModel;
    private $permissionService;
    private $autoUidModel;
    private $attachmentService;
    private $diaryListModel;
    private $diaryListCacheModel;
    private $topicCommentsModel;
    private $commentModel;

    function __construct()
    {
        $this->articleBodyModel         = new ArticleBodyModel();
        $this->articleListModel         = new ArticleListModel();
        $this->articleListCacheModel    = new ArticleListCacheModel();
        $this->articleTagModel          = new ArticleTagModel();
        $this->permissionService        = new PermissionService();
        $this->autoUidModel             = new AutoUidModel();
        $this->attachmentService        = new AttachmentService();
        $this->diaryListModel           = new DiaryListModel();
        $this->diaryListCacheModel      = new DiaryListCacheModel();
        $this->topicCommentsModel       = new TopicCommentsModel();
        $this->commentModel             = new CommentModel();
    }

    public function getArticleInfo($articleBodyId, $author_uid, $uid = '', $isFormat = 0)
    {
        if (empty($articleBodyId))
        {
            AC::$errorNo    =   'ATS003';
            return  FALSE;
        }

        $articleInfo = $this->articleListModel->getArticleInfoByArticleBodyId($articleBodyId, $author_uid);

        if (empty($articleBodyId))
        {
            AC::$errorNo    =   'ATS007';
            AC::$errorMsg   =   '文章不存在或已被删除';
            return  FALSE;
        }

        if($uid) {
            $data = [
                'viewTimes' => $articleInfo['count']['view'] + 1
            ];
            $this->updateArticle($author_uid, $articleBodyId, $data);
        }
        $result = $this->formatArticleData($articleInfo, $isFormat);
        return $result;
    }

    public function createArticle($param){

        //写入数据
        $articleBodyId = $this->runArticle($param);

        //返回数据
        $articleInfo   = $this->getArticleInfo($articleBodyId, $param['uid']);

        // 异步反垃圾分析
        $param['aid'] = $articleInfo['aid'];
        $this->sendArticleToClassify($param);
        RedisPool::addUserEventCreateJob('postArticle', UK::JsonFormat([ 'e'=>'postArticle', 'c'=>['uid'=>intval($articleInfo['uid']), 'bodyId'=>intval($articleInfo['aid'])] ]) );

        return $articleInfo;
    }

    public function editArticle($param){
        //参数过滤
        $param = $this->assembleArticleData($param);
        if(!$param) return false;

        //验证权限
        $access = $this->checkUpdatePermission($param['author_uid'], $param['aid']);
        if(!$access) return false;

        //写入数据
        $articleBodyId = $this->runUpdateArticle($param);

        //返回数据
        $articleInfo   = $this->getArticleInfo($articleBodyId, $param['uid']);

        return $articleInfo;
    }


    /**
     * @param $param
     * @return bool|int|resource
     */
    private function addArticleBody($param)
    {
        if (empty($param['uid']))
        {
            AC::$errorNo    =   'ATS001';
            return  FALSE;
        }
        $param['extras'] = $this->assembleArticleExtend($param);
        $data = $this->filterArticleData($param);
        $result = $this->articleBodyModel->addArticleBody($data);
        return $result;
    }

    /**
     * 过滤文章数据(body)
     * @param $param
     * @return array|bool
     */
    private function filterArticleData($param)
    {
        $data = [];
        $data['uid']          =   isset($param['author_uid']) ? intval($param['author_uid']) : intval($param['uid']);
        $data['updated_at']   =   AC::SERVER_TIME;
        $data['created_at']   =   AC::SERVER_TIME;
        isset($param['title']) && $data['title'] = $param['title'];
        isset($param['cover']) && $data['cover'] = $param['cover'];
        isset($param['content']) && $data['content'] = $param['content'];
        isset($param['category']) && $data['category'] = $param['category'];
        isset($param['reward']) && $data['reward'] = $param['reward'];
        isset($param['extras']) && $data['extras'] = $param['extras'];
        return $data;
    }

    /**
     * 附加信息
     * @param $param
     * @return array|string
     */
    private function assembleArticleExtend($param) {
        $extras = [];

        if(isset($param['modules']['vote'])){
            unset($param['modules']['vote']);
        }
        $attachment = [];
        isset($param['attachment']) && $attachment = array_merge($attachment, $param['attachment']);
        isset($param['voice']) && $attachment = array_merge($attachment, $param['voice']);
        isset($param['video']) && $attachment = array_merge($attachment, $param['video']);
        isset($param['link']) && $attachment = array_merge($attachment, $param['link']);

        isset($param['modules']) && $extras['modules'] = $param['modules'];
        $extras['attachment'] = $attachment;
        isset($param['tag']) && $extras['tag'] = $param['tag'];
        if(isset($param['original']) && $param['original'] != 1){
            $extras['source'] = UK::getstr($param['original'],100, '', 1);
        }

        $extras && $extras = UK::JsonFormat($extras);
        return $extras;
    }


    /**
     * 过滤文章数据(list)
     * @param $param
     * @return array|bool
     */
    private function filterArticleListData($param)
    {
        $data = [];
        $data['uid'] =   isset($param['author_uid']) ? intval($param['author_uid']) : intval($param['uid']);
        $data['aid']    =   $param['aid'];
        isset($param['title'])      && $data['title'] = $param['title'];
        isset($param['cover'])      && $data['cover'] = $param['cover'];
        isset($param['summary'])    && $data['summary'] = $param['summary'];
        isset($param['category'])   && $data['category'] = $param['category'];
        isset($param['vote'])       && $data['vote'] = $param['vote'];
        isset($param['extras'])     && $data['extras'] = $param['extras'];
        if(isset($param['original']) && $param['original'] == 1) {
            $data['int2'] = $param['original'];
        }else{
            $data['int2'] = 0;
        }
        return $data;
    }

    /**
     * 添加文章到列表
     * @param $param
     * @return bool|int|resource
     */
    private function addArticleList($param)
    {
        if (empty($param['uid']) || empty($param['aid']))
        {
            AC::$errorNo    =   'ATS002';
            return  FALSE;
        }
        $param['extras'] = $this->assembleArticleExtend($param);
        $data = $this->filterArticleListData($param);
        $result = $this->articleListModel->addArticleList($data);
        return $result;
    }

    /**
     * 格式化文章详情返回参数
     * @param $param
     * @return array|bool
     */
    private function formatArticleData($param, $isFormat = 0)
    {
        if(empty($param['id']) || empty($param['uid']) || empty($param['aid'])){
            AC::$errorNo    =   'ATS003';
            return false;
        }
        $data['id']                 = strval($param['id']);
        $data['uid']                = strval($param['uid']);
        $data['aid']                = strval($param['aid']);
        $data['title']              = empty($param['title']) ? '' :  $param['title'];
        $data['cover']              = empty($param['cover']) ? '' :  $param['cover'];
        $data['summary']            = empty($param['summary']) ? '' :  $param['summary'];
        $data['content']            = empty($param['content']) ? '' : $param['content'];
        $data['category']           = empty($param['category']) ? '' :  $param['category'];
        $data['original']           = strval($param['original']);
        $data['count'] = [
            'view'      => isset($param['viewTimes']) ? $param['viewTimes'] : 1,
            'comment'   => isset($param['commentTimes']) ? $param['commentTimes'] : 0,
            'repost'    => isset($param['repostTimes']) ? $param['repostTimes'] : 0,
            'like'      => isset($param['likeTimes']) ? $param['likeTimes'] : 0,
            'share'     => isset($param['shareTimes']) ? $param['shareTimes'] : 0,
        ];

        $attachment = $this->buildAttachment($param['attachment']);

        $data['attachment']         = $attachment;
        $data['modules']            = empty($param['modules']) ? [] :  $param['modules'];
        $data['tag']                = empty($param['tag']) ? '' :  $param['tag'];
        $data['modules']['reward']  = empty($extras['modules']['reward']) ? false : true;
        $data['created_at']         = isset($param['created_at']) ? $param['created_at'] : 0;
        $data['updated_at']         = isset($param['updated_at']) ? $param['updated_at'] : 0;
        $data['diaryId']            = isset($param['diaryId'])  ? strval($param['diaryId']) : '0';
        $data['user']               = $this->getArticleDetailAuthor($param['uid'], true);

        if(!empty($data['diaryId']) && !$isFormat){
            $diaryInfo = $this->diaryListModel->getDiaryInfoByBodyId($data['diaryId']);
            //增加话题信息
            $data['topics'] = $this->diaryListModel->getDiaryTopic($diaryInfo['topics']);
            //替换计数
            $result = $this->replaceDiaryTimes($diaryInfo);
            !empty($result) && $data['count'] = $result;
            //获取热门评论信息
            $data['commentHotInfo'] = $this->getDiaryHotComments($data['diaryId'], $diaryInfo['uid'], $diaryInfo['time']);
        }

        return $data;
    }

    /**
     * 获取作者
     * @param unknown $uid
     * @return unknown
     */
    private function getArticleDetailAuthor ($uid, $check_follow = TRUE) {
        if ($check_follow) {
            $user	= $this->autoUidModel->getUserInfo($uid,'',AC::HOST_UID);
        }else{
            $user	= $this->autoUidModel->getUserInfo($uid);
        }
        $user   = $this->autoUidModel->checkIsMe($user);
        // 获取备注名
        $user['nickname']  = $this->autoUidModel->getRemarkName($user);
        return $user;
    }

    /**
     * 获取点滴计数
     * @param $diaryInfo
     * @return array|bool
     */
    private function replaceDiaryTimes($diaryInfo){
        if(empty($diaryInfo)) return false;

        $count = [
            'view'      => isset($diaryInfo['viewTimes'])   ? $diaryInfo['viewTimes']   : 1,
            'comment'   => isset($diaryInfo['commentTimes'])? $diaryInfo['commentTimes']: 0,
            'repost'    => isset($diaryInfo['repostTimes']) ? $diaryInfo['repostTimes'] : 0,
            'like'      => isset($diaryInfo['likeTimes'])   ? $diaryInfo['likeTimes']   : 0,
            'share'     => isset($diaryInfo['shareTimes'])  ? $diaryInfo['shareTimes']  : 0,
        ];
        return $count;
    }

    /**
     * 获取点滴热门评论
     * @param $bodyId
     * @param $uid
     * @param $time
     * @return array|object|StdClass
     */
    private function getDiaryHotComments ($bodyId, $uid, $time) {
        if (empty($bodyId) || empty($uid)) return (Object) array();
        $result = $this->diaryListCacheModel->getHotComment($bodyId);
        if($result){
            $comments = [];
            foreach ($result as $key => $val) {
                $commentInfo = $this->topicCommentsModel->getCommentInfo($val, $bodyId, $uid);
                if(empty($commentInfo)) continue;
                if($commentInfo['status'] != 0) continue;
                $comments[] = $commentInfo;
            }
        }else{
            $comments = $this->topicCommentsModel->getDrabComments($bodyId, $uid, array(0, null, null));
        }
        $result   = $this->commentModel->getCommentsOfDrabs($comments, $time, 1);
        $result = (array)$result;
        $result['comments'] = array_slice($result['comments'], 0, 3);
        if(empty($result['comments'])) {
            return (object)array();
        }
        $result['counts'] = count($result['comments']);
        return $result;
    }

    private function getArticleCategoryIdByName($categoryName){
        $result =  array_search($categoryName, $this->articleBodyModel->articleCategory);
        if(empty($result)){
            return FALSE;
        }
        return $result;
    }

    //获取所有分类
    public function getAllArticleCategory(){
        return $this->articleBodyModel->articleCategory;
    }

    /**
     * 获取用户文章列表
     * @param $uid
     * @param $id
     * @param $length
     * @return array|bool
     */
    public function getUserArticleList($uid, $id, $length){
        $list   =   $this->articleListModel->getUserArticleList($uid, $id, $length);
        if (!empty($list)   &&  is_array($list))
        {
            foreach ($list  as  &$record) {
                if (empty($record)) {
                    continue;
                }
                $record = $this->buildArticleListData($record);
            }
        }
        return $list;
    }

    /**
     * 获取文章列表
     * @param $type
     * @param $id
     * @param $length
     * @return array|bool
     */
    public function getArticleList($type, $id, $length){
        $list   =   $this->articleListModel->getArticleList($type, $id, $length);
        if (!empty($list)   &&  is_array($list))
        {
            foreach ($list  as  &$record) {
                if (empty($record)) {
                    continue;
                }
                $record = $this->buildArticleListData($record);
            }
        }
        return $list;
    }

    /**
     * 格式化文章列表返回参数
     * @param $param
     * @return array
     */
    private function buildArticleListData($param)
    {
        if(empty($param['id']) || empty($param['uid']) || empty($param['aid'])){
            return array();
        }
        $data['id']                 = strval($param['id']);
        $data['uid']                = strval($param['uid']);
        $data['aid']                = strval($param['aid']);
        $data['title']              = empty($param['title']) ? '' :  $param['title'];
        $data['cover']              = empty($param['cover']) ? '' :  $param['cover'];
        $data['summary']            = empty($param['summary']) ? '' :  $param['summary'];
        $data['category']           = empty($param['category']) ? '' :  $this->articleBodyModel->articleCategory[$param['category']];
        $data['diaryId']            = empty($param['int1']) ? '0' :   $param['int1'];
        $data['original']           = empty($param['int2']) ? '0' :   strval($param['int2']);
        $data['created_at']         = empty($param['created_at']) ? '0' : $param['created_at'];
        $data['updated_at']         = empty($param['created_at']) ? '0' : $param['updated_at'];
        $data['count']              = [
            'view'      => empty($param['viewTimes']) ? 0 : $param['viewTimes'],
            'comment'   => empty($param['commentTimes']) ? 0 : $param['commentTimes'],
            'repost'    => empty($param['repostTimes']) ? 0 : $param['repostTimes'],
            'like'      => empty($param['likeTimes']) ? 0 : $param['likeTimes'],
            'share'     => empty($param['shareTimes']) ? 0 : $param['shareTimes'],
        ];

        $extras = UK::parseData($param['extras']);
        $attachment = $this->buildAttachment($extras['attachment']);
        $data['attachment']         = $attachment;

        if(!empty($data['diaryId'])){
            $diaryInfo = $this->diaryListModel->getDiaryInfoByBodyId($data['diaryId']);
            $result = $this->replaceDiaryTimes($diaryInfo);
            !empty($result) && $data['count'] = $result;
        }
        if(empty($data['original']) && isset($extras['source'])){
            $data['original'] = $extras['source'];
        }

        $data['modules'] = $extras['modules'];
        $data['tag']     = $extras['tag'];
        $data['modules']['reward'] = empty($extras['modules']['reward']) ? false : true;
        $data['user'] = $this->getArticleDetailAuthor($data['uid'], true);
        return $data;
    }

    /**
     * 获取文章所有标签
     * @param $articleBodyId
     * @param $type
     * @return array|bool
     */
    public function getArticleTags($articleBodyId, $type){
        $tags = $this->articleTagModel->getArticleAllTags($articleBodyId, $type);
        return $tags;
    }

    /**
     * 删除文章
     *
     * @param $articleBodyId
     * @param $author_uid
     * @return bool
     */
    public function deleteArticle($articleBodyId, $author_uid){
        //验证删除权限
        $status = $this->checkDeletePermission($articleBodyId, $author_uid);
        if(!$status) return false;
        try {
            $this->transactionBegin();

            //删除body
            $result = $this->deleteArticleBody($articleBodyId, $author_uid);
            if (!$result) {
                $this->transactionRollback();
                return false;
            }

            //删除列表
            $result = $this->deleteArticleList($articleBodyId, $author_uid);
            if (!$result)  {
                $this->transactionRollback();
                return false;
            }

            //清除缓存
            $this->articleListCacheModel->delArticleCache($articleBodyId);

            $this->transactionCommit();
        } catch (\Exception $e) {
            $this->transactionRollback();
        }
        return true;
    }

    /**
     * 验证删除文章权限
     *
     * @param $articleBodyId
     * @param $author_uid
     * @return bool
     */
    private function checkDeletePermission($articleBodyId, $author_uid) {
        //验证文章是否存在
        $articleInfo = $this->articleListModel->getArticleInfoByArticleBodyId($articleBodyId, $author_uid);
        if(empty($articleInfo)) {
            AC::$errorNo    =   'ATS007';
            AC::$errorMsg    =   '文章不存在或已被删除';
            return false;
        }

        //验证用户是否有删除权限(暂时先验证当前用户是否是作者)
        $status = $this->permissionService->checkUserIsHostUser($author_uid);
        if (!$status) {
            AC::$errorNo    =   'ATS005';
            AC::$errorMsg    =   '当前用户不是作者';
            return false;
        }

        return true;
    }

    /**
     * 删除文章body
     * @param $articleBodyId
     * @param $author_uid
     * @return bool|int|resource
     */
    private function deleteArticleBody($articleBodyId, $author_uid){

        $result = $this->articleBodyModel->delArticleBody($author_uid, $articleBodyId);
        return $result;
    }

    /**
     * 删除文章list
     * @param $articleBodyId
     * @param $author_uid
     * @return bool|int|resource
     */
    private function deleteArticleList($articleBodyId, $author_uid){
        $result = $this->articleListModel->delArticleList($author_uid, $articleBodyId);
        return $result;
    }

    /**
     * 更新文章
     * @param $param
     * @return bool|int|resource
     */
    private function updateArticleBody($param)
    {
        if (empty($param['uid']))
        {
            AC::$errorNo    =   'ATS001';
            return  FALSE;
        }
        $param['extras'] = $this->assembleArticleExtend($param);
        $data = $this->filterArticleData($param);
        $result = $this->articleBodyModel->updateArticleBody($data, $param['aid']);
        return $result;
    }

    /**
     * 更新文章列表
     * @param $param
     * @return bool|int|resource
     */
    private function updateArticleList($param){
        if (empty($param['author_uid']) || empty($param['aid']))
        {
            AC::$errorNo    =   'ATS002';
            return  FALSE;
        }
        $param['extras'] = $this->assembleArticleExtend($param);
        $data = $this->filterArticleListData($param);
        $result = $this->articleListModel->updateArticleList($data);
        return $result;
    }

    /**
     * 验证创建文章相关权限
     * @param $uid
     * @return bool
     */
    public function checkPermission($uid)
    {
        if(empty($uid)) return false;
        $hostStatus = $this->permissionService->checkUserIsHostUser($uid);
        if (!$hostStatus) {
            AC::$errorNo    =   'ATS005';
            AC::$errorMsg    =   '当前用户不是作者';
            return false;
        }

        //检查是否被封禁
        $blockStatus = $this->permissionService->checkUserIsBlocked($uid);
        if ($blockStatus) {
            AC::$errorNo    =   'ATS006';
            AC::$errorMsg    =   '用户被封禁';
            return false;
        }

        return true;
    }

    /**
     * 验证更新文章相关权限
     * @param $author_uid
     * @param $uid
     * @param $articleBodyId
     * @return bool
     */
    private function checkUpdatePermission($author_uid, $articleBodyId, $uid = AC::HOST_UID)
    {
        if($author_uid  != $uid){
            AC::$errorNo    =   'ATS007';
            AC::$errorMsg    =   '没有权限';
            return false;
        }

        //检查当前用户是否被封禁
        $blockStatus = $this->permissionService->checkUserIsBlocked($uid);
        if ($blockStatus) {
            AC::$errorNo    =   'ATS006';
            AC::$errorMsg    =   '用户被封禁';
            return false;
        }

        //验证文章是否存在
        $article = $this->articleListModel->getArticleInfoByArticleBodyId($articleBodyId, $author_uid);
        if (empty($article)) {
            AC::$errorNo    =   'ATS007';
            AC::$errorMsg    =   '文章不存在';
            return false;
        }

        return true;
    }


    /**
     * 文章参数初步过滤
     * @param $param
     * @return bool
     */
    public function assembleArticleData($param) {
        if(empty($param)) return $param;
        if(empty($param['uid'])) $param['uid'] = AC::HOST_UID;

        //文章HTML标签过滤
        $content = UK::checkHTMLContent(urlencode($param['content']), AC::$articleHtmlAllowed);
        $param['content'] = urldecode($content);
        //文章长度统一
        $max_numbers = AC::ARTICLECONTENTLENGTH;
        // 内容过滤
        $param['content'] = UK::getstr($param['content'], $max_numbers, '', 3);
        if(isset($param['summary']) && !empty($param['summary'])) {
            $param['summary'] = UK::getstr($param['summary'], AC::ARTICLESUMMARYLENGTH, '', 3);
        }
        $param['title'] = UK::getstr($param['title'], AC::ARTICLETITLELENGTH, '', 3);
        //标题、题图、内容不能为空
        if(strlen($param['content']) < 1 || strlen($param['cover']) < 1 || strlen($param['title']) < 1){
            AC::$errorNo    =   'ATS004';
            return false;
        }
        //来源处理
        if(isset($param['source']) && $param['source'] > 0) {
            $param['original'] = 1;
        }
        if(empty($param['original'])) {
            $param['original'] = '未知';
        }
        //分类处理
        $param['category'] = $this->getArticleCategoryIdByName($param['category']);
        if(empty($param['category'])) $param['category'] = 1;
        // reward字段处理
        $param['reward'] = isset($param['reward'])  && $param['reward'] == true ? 1 : 0;

        //附件处理
        $param = $this->attachmentService->classifyAttachments($param);

        return $param;
    }

    /**
     * 新建保存
     * @param $param
     * @return bool
     */
    private function runArticle($param){
        if(empty($param)) {
            AC::$errorNo    =   'ATS004';
            return false;
        }

        //addArticleBody
        $articleItemId = $this->addArticleBody($param);
        if(empty($articleItemId)) return false;

        //生成bodyId
        $articleBodyId = $this->articleBodyModel->getArticleBodyId($param['uid'],$articleItemId);
        $param['aid'] = $articleBodyId;

        // 文章标签处理
        $param['tag'] = $this->articleTagModel->editArticleTags($param['aid'], $param['tag']);

        //addAttachment
        $this->addArticleAttachment($param);

        //addArticleList
        $articleListInsert=   $this->addArticleList($param);
        if(empty($articleListInsert)) return false;

        // 更新用户活跃时间
        $this->autoUidModel->updateUserLastActivity($param['uid']);

        return $articleBodyId;
    }

    /**
     * 附件数据分类处理
     * @param $attachment
     * @return array
     */
    public function buildAttachment($attachment){
        if(empty($attachment)) return $attachment;
        $attachmentList = [];
        foreach ($attachment as $item) {
            if(empty($item['guid'])) {
                continue;
            }
            switch ($item['attachmentType']) {
                case 1:
                    $type = 'image';
                    break;
                case 2:
                    $type = 'audio';
                    break;
                case 3:
                    $type = 'video';
                    break;
                case 4:
                    $type = 'share';
                    break;
                default:
                    $type = 'image';
                    break;
            }
            $tmp['guid']    = $item['info']['guid'];
            $tmp['src']     = $item['path'];
            $tmp['alt']     = $item['info']['alt'];
            $tmp['type']    = $type;
            $tmp['info']    = $item['info']['info'];
            $attachmentList[] = $tmp;
        }
        return $attachmentList;
    }

    /**
     * 保存文章附件
     * @param $param
     * @return boolean
     */
    private function addArticleAttachment ($param) {
        if (AC::$errorNo) return FALSE;
        $param['bodyId'] = $param['aid'];
        $param['type']    = AC::ARTICLE_ATTACHMENT_TYPE;
        $param['type_id'] = $param['bodyId'];

        $userService = new UserService();
        $user = $userService->getUserInfo($param['uid'], AC::HOST_UID);
        $result = $this->attachmentService->saveDiaryAttachment($param, $user);
        return $result;
    }


    /**
     * 更新
     * @param $param
     * @return bool
     */
    private function runUpdateArticle($param){
        if(empty($param)) {
            return false;
        }

        //更新articleBody
        $status =  $this->updateArticleBody($param);
        if (empty($status)) return false;

        // 文章标签处理
        $param['tag'] = $this->articleTagModel->editArticleTags($param['aid'], $param['tag']);

        // 写附件信息
        $result = $this->attachmentService->delAttachment($param['author_uid'], $param['aid'], AC::ARTICLE_ATTACHMENT_TYPE);
        if(!$result) return false;

        $this->addArticleAttachment($param);

        //加入列表
        $articleListInsert=   $this->updateArticleList($param);
        if (empty($articleListInsert)) return false;

        //更新缓存
        $this->articleListCacheModel->delArticleCache($param['aid']);
        $this->articleListModel->getArticleInfoByArticleBodyId($param['aid'], $param['author_uid']);

        // 更新用户活跃时间
        $this->autoUidModel->updateUserLastActivity($param['uid']);

        return $param['aid'];
    }

    /**
     * 更新文章
     * @param $uid
     * @param $articleBodyId
     * @param $param
     */
    public function updateArticle($uid, $articleBodyId, $param){
        $data = [];
        isset($param['viewTimes'])      && $data['viewTimes']       = $param['viewTimes'];
        isset($param['commentTimes'])   && $data['commentTimes']    = $param['commentTimes'];
        isset($param['repostTimes'])    && $data['repostTimes']     = $param['repostTimes'];
        isset($param['likeTimes'])      && $data['likeTimes']       = $param['likeTimes'];
        isset($param['shareTimes'])     && $data['shareTimes']      = $param['shareTimes'];
        isset($param['diaryId'])        && $data['diaryId']         = $param['diaryId'];
        $this->articleListCacheModel->addArticleCache($articleBodyId, $data);

        isset($data['diaryId'])         && $data['int1']            = $data['diaryId'];
        unset($data['diaryId']);
        $data['aid'] = $articleBodyId;
        $data['uid'] = $uid;
        $this->articleListModel->updateArticleList($data);
    }

    private function getArticleAttachment($uid, $articleBodyId) {
        if(empty($uid) || empty($articleBodyId)) return false;
        $type = AC::ARTICLE_ATTACHMENT_TYPE;
        $attachmentList = $this->attachmentService->getAttachment($uid, $type, $articleBodyId);
        $attachment = [];
        $guids = [];
        foreach ($attachmentList as $item) {
            $info = UK::parseData($item['info']);
            $tmp = [
                'guid'       => $info['guid'],
                'alt'       => $info['alt'],
                'info'      => $info['info'],
                'src'       => $item['attachmentPath'],
                'type'      => $item['type']
            ];
            $attachment[] = $tmp;
            $guids[] = $info['guid'];
        }
        return ['attachment' => $attachment, 'guids' => $guids];
    }

    /**
     * 反垃圾收集
     * @param $bodyInfo
     */
    private function sendArticleToClassify ($bodyInfo) {
        $classifyData = array(
            'uid'         =>    strval($bodyInfo['uid']),
            'target'      =>    'article',
            'targetId'    =>    strval($bodyInfo['aid']),
            'text'        =>    array(
                'title'	  =>	strval($bodyInfo['title']),
                'content' =>	strval($bodyInfo['content'])
            ),
            'attachments' =>	array(
                'images'  =>	$bodyInfo['attachment'],
                'voices'  =>	$bodyInfo['voice']
            ),
            'extra'       =>    array(
                'bodyId'  =>    strval($bodyInfo['aid'])
            ),
            'version'     =>    AC::$antispam_version
        );
        $classifyDataOfJson = json_encode($classifyData);
        RedisPool::addClassifyGearmanJob("classify", $classifyDataOfJson);
    }


    /**
     * 事务开启
     */
    private function transactionBegin()
    {
        $this->articleBodyModel->begin();
    }

    /**
     * 事务提交
     */
    private function transactionCommit()
    {
        $this->articleBodyModel->commit();
    }

    /**
     * 事务回滚
     */
    private function transactionRollback()
    {
        $this->articleBodyModel->rollback();
    }

}